package com.engine.hitech.duss.budget.utils;

import com.wbi.util.StringUtil;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-08-04
 * @Description: ${description}
 * @ModifyLog:
 **/

public class BgUtil {

    public static void main(String[] args) {
        int[] arr = {1000, 2000};
        for (int subcompanyid : arr) {
            System.out.println( subcompanyid );
            String subCompanyname = 1000 == subcompanyid ? "封测PKG/T" : "模组Module";
            System.out.println( subCompanyname );
        }
        ;

    }


    /**
     * @Description: 获取指定引用版本
     * @Param: []
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2022/8/4
     */
    public static String getId2Zdyybb(String id) {
        RecordSet rs = new RecordSet();
        rs.executeQuery( "select bbh from uf_bzfa where id=?", id );
        rs.next();
        return Util.null2String( rs.getString( "bbh" ) );
    }

    /**
     * @Description: 获取指定版本任务数据
     * @Param: []
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2022/8/4
     */
    public static String getFormid(String appid) {
        RecordSet rs = new RecordSet();
        String formids = "";
        rs.executeQuery( "select t.formid from edc_pagesheet t " +
                "left join edc_page t1 on t.pageid=t1.id where t1.appid=?", appid );
        while (rs.next()) {
            String formid = Util.null2String( rs.getString( 1 ) );
            formids += "".equals( formids ) ? formid : "," + formid;
        }
        return formids;
    }

    /**
     * @Description: 获取指定版本任务数据
     * @Param: []
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2022/8/4
     */
    public static String getSheetName(String formid) {
        RecordSet rs = new RecordSet();
        rs.executeQuery( "select name from edc_pagesheet where formid=?", formid );
        rs.next();
        return Util.null2String( rs.getString( 1 ) );
    }

    /**
     * @Description: 获取数据表的明细表
     * @Param: []
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2022/8/4
     */
    public static String getMainTable(String formid) {
        RecordSet rs = new RecordSet();
        rs.executeQuery( "select tablename from workflow_bill where id=?", formid );
        rs.next();
        return Util.null2String( rs.getString( "tablename" ) );
    }

    /**
     * @Description: 获取数据表的明细表
     * @Param: []
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2022/8/4
     */
    public static List<Map<String, String>> getDetailTable(String formid) {
        RecordSet rs = new RecordSet();
        List<Map<String, String>> mapList = new ArrayList<>();
        rs.executeQuery( "select orderid,tablename from Workflow_billdetailtable  where billid =? order by tablename", formid );
        while (rs.next()) {
            Map<String, String> dataMap = new HashMap<>();
            String orderid = Util.null2String( rs.getString( "orderid" ) );
            String tablename = Util.null2String( rs.getString( "tablename" ) );
            dataMap.put( "orderid", orderid );
            dataMap.put( "tablename", tablename );
            mapList.add( dataMap );
        }
        return mapList;
    }

    /**
     * @Description: 获取任务最新版本号
     * @Param: []
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2022/8/4
     */
    public static String getTaskZxbbh() {
        RecordSet rs = new RecordSet();
        rs.executeQuery( "select max(bbh) from edc_task" );
        rs.next();
        return Util.null2String( rs.getString( 1 ) );
    }

    /**
     * @Description: 获取最新版本号
     * @Param: []
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2022/8/4
     */
    public static Map<String, String> getZxBbhAndId(String bbh) {
        RecordSet rs = new RecordSet();
        Map<String, String> map = new HashMap<>();
        rs.executeQuery( "select id from uf_bzfa where bbh=?", bbh );
        rs.next();
        String id = Util.null2String( rs.getString( 1 ) );
        map.put( "id", id );
        map.put( "bbh", bbh );
        return map;
    }

    /**
     * @Description: 获取版本号
     * @Param: []
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2022/8/4
     */
    public static String getBbhById(String id) {
        RecordSet rs = new RecordSet();
        rs.executeQuery( "select bbh from uf_bzfa where id=?", id );
        rs.next();
        return Util.null2String( rs.getString( 1 ) );
    }

    /**
     * @Description: 获取上一个版本号
     * @Param: []
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2022/8/4
     */
    public static String getSygbbh(String bbh) {
        RecordSet rs = new RecordSet();
        rs.executeQuery( "select max(bbh) from uf_bzfa where bbh !=?", bbh );
        rs.next();
        String sygbbh = Util.null2String( rs.getString( 1 ) );
        return sygbbh;
    }

    /**
     * @Description: 根据appid和版本号获取任务ID
     * @Param: []
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2022/8/4
     */
    public static String getTaskIdBybbh(String appid, String bbh) {
        RecordSet rs = new RecordSet();
        rs.executeQuery( "select max(id) from edc_task where appid=? and bbh=? and isfinished=1",
                appid, bbh );
        rs.next();
        String taskid = Util.null2String( rs.getString( 1 ) );
        return taskid;
    }

    /**
     * @Description: 获取任务节点ID
     * @Param: []
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2022/8/4
     */
    public static String getNodeid(String taskid) {
        RecordSet rs = new RecordSet();
        rs.executeQuery( " select max(id) from edc_tasknode where taskid=?", taskid );
        rs.next();
        return Util.null2String( rs.getString( 1 ) );
    }

    /**
     * @Description: 获取汇总表状态
     * @Param: []
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2022/8/4
     */
    public static Map<String, Object> getSumStataus(String bbh_param, String orders) {
        RecordSet rs = new RecordSet();
        Map<String, Object> dataMap = new HashMap<>();
        String sql = "select id,bzzt from view_custom_task " +
                "where bzzt in ('未编制','编制中','已审核') and bbh='" + bbh_param + "'";
        if (StringUtil.isNotBlank( orders )) {
            sql += BgUtil.converOrders( orders );
        }
        rs.executeQuery( sql );
        String taskids = "";
        int wbzcount = 0;
        int bzzcount = 0;
        int yshcount = 0;
        int counts = rs.getCounts();
        while (rs.next()) {
            String taskid = Util.null2String( rs.getString( "id" ) );
            String bzzt = Util.null2String( rs.getString( "bzzt" ) );
            switch (bzzt) {
                case "未编制":
                    wbzcount++;
                    break;
                case "编制中":
                    bzzcount++;
                    break;
                case "已审核":
                    yshcount++;
                    taskids += "".equals( taskids ) ? taskid : "," + taskid;
                    break;
                default:
            }
        }
        dataMap.put( "counts", counts );
        dataMap.put( "wbzcount", wbzcount );
        dataMap.put( "bzzcount", bzzcount );
        dataMap.put( "yshcount", yshcount );
        dataMap.put( "taskids", taskids );
        return dataMap;
    }

    /**
     * @Description: 创建部门科目汇总表流程
     * @Param: []
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2022/8/4
     */
    public static Map<String, Object> createBmkmhzWorkflow(String workflowid, String bmbm, String taskids, int userid) throws Exception {
        RecordSet rs = new RecordSet();
        Map<String, Object> dataMap = new HashMap<>();
        String workflowname = getWorkflowName( workflowid );
        String curdate = DateUtil.getCurrentDate();
        String orders = getOrdersByDepart( bmbm );
        //科室科目汇总流程
        String sql = "select bbh,orders from view_custom_task where id in (" + taskids + ") and orders='" + orders + "'" +
                "group by bbh,orders";
        rs.executeQuery( sql );
        int totalcount = rs.getCounts();
        int successcount = 0;
        int failurecount = 0;
        if (rs.next()) {
            String bbh = Util.null2String( rs.getString( "bbh" ) );
            ResourceComInfo resourceComInfo = new ResourceComInfo();
            Map<String, String> requestMainMap = new HashMap<>();
            String requestname = workflowname + "_" + curdate + "_" + resourceComInfo.getLastname( userid + "" );
            requestMainMap.put( "ysfa", getBzfaByBbh( bbh ).get( "id" ) );
            requestMainMap.put( "bznf", getBzfaByBbh( bbh ).get( "bznf" ) );
            requestMainMap.put( "bzksyf", getBzfaByBbh( bbh ).get( "bzksyf" ) );
            requestMainMap.put( "lsbbh", getBzfaByBbh( bbh ).get( "zdyybb" ) );
            requestMainMap.put( "bbh", bbh );
            requestMainMap.put( "order1", getIdByOrders( orders ) );
            requestMainMap.put( "bmbm", getDepartIdByCode( bmbm ) );
            WorkflowCreateHandler workflowCreateHandler = new WorkflowCreateHandler();
            String requestid = workflowCreateHandler.WorkflowCreateByRequestMap( userid + "", workflowid, requestname, "0", requestMainMap );
            rs.writeLog( ">>>>>getTypeByCreateWf>>>requestid=" + requestid );
            if (Util.getIntValue( requestid ) > 0) {
                successcount++;
            } else {
                failurecount++;
            }
        }
        dataMap.put( "totalcount", totalcount );
        dataMap.put( "failurecount", failurecount );
        dataMap.put( "successcount", successcount );
        return dataMap;
    }

    /**
     * @Description: 创建科室科目汇总表流程
     * @Param: []
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2022/8/4
     */
    public static Map<String, Object> createKskmhzWorkflow(String workflowid, String bmbm, String ksbm, String taskids, int userid) throws Exception {
        RecordSet rs = new RecordSet();
        Map<String, Object> dataMap = new HashMap<>();
        String workflowname = getWorkflowName( workflowid );
        String curdate = DateUtil.getCurrentDate();
        String ksid = getDepartIdByCode( ksbm );
        //科室科目汇总流程
        String sql = "select (select STUFF((select ','+cast(id as varchar) from view_custom_task " +
                "where id in (" + taskids + ") and syksid='" + ksid + "' for xml path('')),1,1,'')) as ids," +
                "bbh,orders,syksid from view_custom_task where id in (" + taskids + ") " +
                "and syksid='" + ksid + "' group by bbh,orders,syksid";
        rs.executeQuery( sql );
        int totalcount = rs.getCounts();
        int successcount = 0;
        int failurecount = 0;
        if (rs.next()) {
            String ids = Util.null2String( rs.getString( "ids" ) );
            String orders = Util.null2String( rs.getString( "orders" ) );
            String bbh = Util.null2String( rs.getString( "bbh" ) );
            ResourceComInfo resourceComInfo = new ResourceComInfo();
            Map<String, String> requestMainMap = new HashMap<>();
            String requestname = workflowname + "_" + curdate + "_" + resourceComInfo.getLastname( userid + "" );
            requestMainMap.put( "ysfa", Util.null2String( getBzfaByBbh( bbh ).get( "id" ) ) );
            requestMainMap.put( "bznf", Util.null2String( getBzfaByBbh( bbh ).get( "bznf" ) ) );
            requestMainMap.put( "bzksyf", Util.null2String( getBzfaByBbh( bbh ).get( "bzksyf" ) ) );
            requestMainMap.put( "lsbbh", Util.null2String( getBzfaByBbh( bbh ).get( "zdyybb" ) ) );
            requestMainMap.put( "bbh", bbh );
            requestMainMap.put( "order1", getIdByOrders( orders ) );
            requestMainMap.put( "ksmc", Util.null2String( rs.getString( "syksid" ) ) );
            requestMainMap.put( "ksbm", ksbm );
            requestMainMap.put( "bmbm", bmbm );
            WorkflowCreateHandler workflowCreateHandler = new WorkflowCreateHandler();
            String requestid = workflowCreateHandler.WorkflowCreateByRequestMap( userid + "", workflowid, requestname, "0", requestMainMap );
            rs.writeLog( ">>>>>getTypeByCreateWf>>>requestid=" + requestid );
            if (Util.getIntValue( requestid ) > 0) {
                successcount++;
            } else {
                failurecount++;

            }
            rs.executeUpdate( "update edc_task set requestid='" + requestid + "' where id in (" + ids + ")" );
        }
        dataMap.put( "totalcount", totalcount );
        dataMap.put( "failurecount", failurecount );
        dataMap.put( "successcount", successcount );
        return dataMap;
    }

    /**
     * @Description: 获取流程名称
     * @Param: [workflowid]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2022/8/24
     */
    public static String getWorkflowName(String workflowid) {
        RecordSet rs = new RecordSet();
        rs.executeQuery( "select workflowname from workflow_base where id =?", workflowid );
        rs.next();
        return Util.null2String( rs.getString( "workflowname" ) );
    }

    /**
     * @Description: 根据部门编码转orders
     * @Param: [workflowid]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2022/8/24
     */
    public static String getOrdersByDepart(String bmbm) {
        RecordSet rs = new RecordSet();
        rs.executeQuery( "select distinct orders from view_custom_depart where sjbmbm=?", bmbm );
        rs.next();
        return Util.null2String( rs.getString( 1 ) );
    }

    /**
     * @Description: 根据orders获取主数据Id
     * @Param: [workflowid]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2022/8/24
     */
    public static String getIdByOrders(String orders) {
        RecordSet rs = new RecordSet();
        rs.executeQuery( "  select id from uf_order where AUFNR =?", orders );
        rs.next();
        return Util.null2String( rs.getString( 1 ) );
    }

    /**
     * @Description: 根据部门编码转为部门ID
     * @Param: [departmentcode]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2022/8/24
     */
    public static String getDepartIdByCode(String departmentcode) {
        RecordSet rs = new RecordSet();
        rs.executeQuery( "select id from HrmDepartment where departmentcode=?", departmentcode );
        rs.next();
        return Util.null2String( rs.getString( "id" ) );
    }

    /**
     * @Description: 拆解orders参数
     * @Param: [workflowid]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2022/8/24
     */
    public static String converOrders(String ordersParam) {
        String sqlwhere = "";
        String[] strs = ordersParam.split( "_" );
        if ("5".equals( strs[0] )) {
            String ksids =getKsbm(strs[1],strs[0]);
            sqlwhere = "and syksid = '" + ksids + "'";
        } else if ("3".equals( strs[0] ) || "4".equals( strs[0] )) {
            String ksids =getKsbm(strs[1],strs[0]);
            sqlwhere = "and syksid in (" + ksids + ")";
        } else {
            sqlwhere = "and 1 =0";
        }
        return sqlwhere;
    }

    /**
     * @Description: 获取部门科室
     * @Param: []
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2022/8/4
     */
    public static String getKsbm(String code, String tlevel) {
        RecordSet rs = new RecordSet();
        String ksids = "";
        String sqlwhere = "5".equals( tlevel ) ? " and id=?" : " and supdepid=?";
        String sql = "select departmentcode from HrmDepartment where isnull(departmentcode,'') !=''" + sqlwhere;
        rs.executeQuery( sql, code );
        while (rs.next()) {
            String departmentcode = Util.null2String( rs.getString( "departmentcode" ) );
            String ksid = getDepartIdByCode( departmentcode );
            ksids += "".equals( ksids ) ? ksid : "," + ksid;
        }
        return ksids;
    }

    /**
     * @Description: 根据版本号获取编制方案信息
     * @Param: []
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2022/8/4
     */
    public static Map<String, String> getBzfaByBbh(String bbh) {
        RecordSet rs = new RecordSet();
        Map<String, String> map = new HashMap<>();
        rs.executeQuery( "select id,bznf,zdyybb,bzksyf from uf_bzfa where bbh=?", bbh );
        rs.next();
        map.put( "id", Util.null2String( rs.getString( "id" ) ) );
        map.put( "bznf", Util.null2String( rs.getString( "bznf" ) ) );
        map.put( "zdyybb", getBbhById( Util.null2String( rs.getString( "zdyybb" ) ) ) );
        map.put( "bzksyf", Util.null2String( rs.getString( "bzksyf" ) ) );
        return map;
    }
}
